package com.zhangx.remote.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhangx.remote.dto.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * @ClassName RemoteOrderController
 * @Description 订单服务接口
 * @Author zhangx
 * @Date 2020/6/17 14:27
 **/
@RestController
@RequestMapping("/order")
@Slf4j
public class RemoteOrderController {
    /**
    * @Description 确认订单
    * @Date 2020/6/17 14:29
    * @return com.alibaba.fastjson.JSONObject
    **/
    @PostMapping("confirm")
    public JSONObject confirmOrder(@RequestBody OrderInfo orderInfo){
        log.info(JSON.toJSONString(orderInfo));
        JSONObject result = new JSONObject();
        result.put("orderNo",orderInfo.getOrderNo());
        result.put("id",orderInfo.getId());
        result.put("pay",new Random().nextInt(9999));
        return result;
    }

    /**
     * @Description 批量确认订单
     * @Date 2020/6/17 14:59
     * @return com.alibaba.fastjson.JSONObject
     **/
    @PostMapping("confirm/batch")
    public JSONArray confirmOrderBatch(@RequestBody List<OrderInfo> orderInfos){
        JSONArray array = new JSONArray();
        for (int i = 0; i < orderInfos.size(); i++) {
            OrderInfo orderInfo = orderInfos.get(i);
            JSONObject result = new JSONObject();
            result.put("orderNo",orderInfo.getOrderNo());
            result.put("id",orderInfo.getId());
            result.put("pay",new Random().nextInt(9999));
            array.add(result);
        }
        return array;
    }
}
